//
//  TestTabbarController.m
//  testApp
//
//  Created by iMac on 2018/2/27.
//  Copyright © 2018年 iMac. All rights reserved.
//

#import "TestTabbarController.h"
#import "StudentRedController.h"
#import "StudentBlueController.h"
#import "StudentGreenController.h"
#import "TeacherRedController.h"
#import "TeacherBlueController.h"
#import "TeacherGreenController.h"
#import "ChangeRoleController.h"



@interface TestTabbarController () <UITabBarControllerDelegate>

@end

@implementation TestTabbarController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self setViewControllers:[self controllersForStudent]];
    self.selectedIndex = 0;
    self.delegate = self;
    self.role = UserRoleStudent;
    
}

- (void)changeUserRole:(UserRole)role {
    
    
    if (role == UserRoleStudent) {
        [self setViewControllers:[self controllersForStudent]];
    }
    else if (role == UserRoleTeacher) {
        [self setViewControllers:[self controllersForTeacher]];
    }
    self.role = role;
}


- (NSArray<UIViewController *> *)controllersForStudent {

    UINavigationController *red = [[UINavigationController alloc] initWithRootViewController:[[StudentRedController alloc] init]];
    UITabBarItem *redItem = [[UITabBarItem alloc] initWithTitle:@"S_R" image:[self imageWithColor:[UIColor redColor]] tag:0];
    [red setTabBarItem:redItem];
    
    UINavigationController *blue = [[UINavigationController alloc] initWithRootViewController:[[StudentBlueController alloc] init]];
    UITabBarItem *blueItem = [[UITabBarItem alloc] initWithTitle:@"S_B" image:[self imageWithColor:[UIColor blueColor]] tag:1];
    [blue setTabBarItem:blueItem];
    
    UINavigationController *green = [[UINavigationController alloc] initWithRootViewController:[[StudentGreenController alloc] init]];
    UITabBarItem *greenItem = [[UITabBarItem alloc] initWithTitle:@"S_G" image:[self imageWithColor:[UIColor redColor]] tag:2];
    [green setTabBarItem:greenItem];
    
    return @[red, blue, green];
}
- (NSArray<UIViewController *> *)controllersForTeacher {
    
    UINavigationController *red = [[UINavigationController alloc] initWithRootViewController:[[TeacherRedController alloc] init]];
    UITabBarItem *redItem = [[UITabBarItem alloc] initWithTitle:@"T_R" image:[self imageWithColor:[UIColor redColor]] tag:0];
    [red setTabBarItem:redItem];
    
    UINavigationController *blue = [[UINavigationController alloc] initWithRootViewController:[[TeacherBlueController alloc] init]];
    UITabBarItem *blueItem = [[UITabBarItem alloc] initWithTitle:@"T_B" image:[self imageWithColor:[UIColor blueColor]] tag:1];
    [blue setTabBarItem:blueItem];
    
    UINavigationController *green = [[UINavigationController alloc] initWithRootViewController:[[TeacherGreenController alloc] init]];
    UITabBarItem *greenItem = [[UITabBarItem alloc] initWithTitle:@"T_G" image:[self imageWithColor:[UIColor redColor]] tag:2];
    [green setTabBarItem:greenItem];
    
    return @[red, blue, green];
}


#pragma mark UITabbarControllerDelegate
//- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
//    NSLog(@"tabBarController.selectedIndex : %ld", tabBarController.selectedIndex);
//    if (tabBarController.selectedIndex == 2) {
//        [self presentViewController:[ChangeRoleController new] animated:YES completion:nil];
//    }
//}
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (viewController.tabBarItem.tag == 2) {
        UINavigationController *target = [[UINavigationController alloc] initWithRootViewController:[ChangeRoleController new]];
        [self presentViewController:target animated:YES completion:nil];
        return NO;
    }
    return YES;
}


- (UIImage *)imageWithColor:(UIColor *)color {
    CGRect rect = CGRectMake(0.f, 0.f, 1.f, 1.f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

@end
